package com.fr.cell.core;

import com.fr.util.Utils;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.io.Serializable;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class JNumberField extends JTextField
  implements Serializable
{
  public static final double ERROR_VALUE = 1.7976931348623157E+308D;
  private int maxIntegerLength;
  private int maxDecimalLength;
  private double minValue;
  private double maxValue;
  private boolean isContentChanged;

  public JNumberField()
  {
    this(32, 16);
  }

  public JNumberField(int paramInt)
  {
    super(paramInt);
    this.maxIntegerLength = 24;
    this.maxDecimalLength = 16;
    this.minValue = -1.797693134862316E+308D;
    this.maxValue = 1.7976931348623157E+308D;
    this.isContentChanged = false;
  }

  public JNumberField(int paramInt1, int paramInt2)
  {
    this(paramInt1, paramInt2, -1.797693134862316E+308D, 1.7976931348623157E+308D);
  }

  public JNumberField(int paramInt1, int paramInt2, double paramDouble1, double paramDouble2)
  {
    this.maxIntegerLength = 24;
    this.maxDecimalLength = 16;
    this.minValue = -1.797693134862316E+308D;
    this.maxValue = 1.7976931348623157E+308D;
    this.isContentChanged = false;
    this.maxIntegerLength = paramInt1;
    this.maxDecimalLength = paramInt2;
    this.minValue = paramDouble1;
    this.maxValue = paramDouble2;
    setDocument(new NumberDocument(this));
  }

  public int getMaxIntegerLength()
  {
    return this.maxIntegerLength;
  }

  public void setMaxIntegerLength(int paramInt)
  {
    this.maxIntegerLength = paramInt;
  }

  public int getMaxDecimalLength()
  {
    return this.maxDecimalLength;
  }

  public void setMaxDecimalLength(int paramInt)
  {
    this.maxDecimalLength = paramInt;
  }

  public double getMinValue()
  {
    return this.minValue;
  }

  public void setMinValue(double paramDouble)
  {
    this.minValue = paramDouble;
  }

  public double getMaxValue()
  {
    return this.maxValue;
  }

  public void setMaxValue(double paramDouble)
  {
    this.maxValue = paramDouble;
  }

  public void setValue(double paramDouble)
  {
    setText(Utils.doubleToString(paramDouble));
  }

  public double getValue()
    throws NumberFormatException
  {
    try
    {
      if (getText().length() == 0)
        return 0.0D;
      return Double.parseDouble(getText());
    }
    catch (NumberFormatException localNumberFormatException)
    {
    }
    return 1.7976931348623157E+308D;
  }

  public String getTextValue()
  {
    return getText();
  }

  public void setInteger(boolean paramBoolean)
  {
    if (paramBoolean)
      this.maxDecimalLength = 0;
    else
      this.maxDecimalLength = 16;
  }

  public boolean isContentChanged()
  {
    return this.isContentChanged;
  }

  public Dimension getPreferredSize()
  {
    return new Dimension(60, 22);
  }

  public static void main(String[] paramArrayOfString)
  {
    JFrame localJFrame = new JFrame("");
    localJFrame.setSize(400, 320);
    Dimension localDimension = Toolkit.getDefaultToolkit().getScreenSize();
    localJFrame.setLocation((localDimension.width - localJFrame.getSize().width) / 2, (localDimension.height - localJFrame.getSize().height) / 2);
    localJFrame.getContentPane().setLayout(new GridLayout(10, 2));
    localJFrame.getContentPane().add(new JLabel("New JNumberField()"));
    localJFrame.getContentPane().add(new JNumberField());
    localJFrame.getContentPane().add(new JLabel("New JNumberField(2)"));
    localJFrame.getContentPane().add(new JLabel("New JNumberField(8,2)"));
    localJFrame.getContentPane().add(new JNumberField(8, 2));
    localJFrame.getContentPane().add(new JLabel("New JNumberField(5,2,-10,100)"));
    localJFrame.getContentPane().add(new JNumberField(5, 2, -10.0D, 100.0D));
    localJFrame.setVisible(true);
  }

  static int access$000(JNumberField paramJNumberField)
  {
    return paramJNumberField.maxDecimalLength;
  }

  static int access$100(JNumberField paramJNumberField)
  {
    return paramJNumberField.maxIntegerLength;
  }

  static double access$200(JNumberField paramJNumberField)
  {
    return paramJNumberField.minValue;
  }

  static double access$300(JNumberField paramJNumberField)
  {
    return paramJNumberField.maxValue;
  }

  static boolean access$402(JNumberField paramJNumberField, boolean paramBoolean)
  {
    return (paramJNumberField.isContentChanged = paramBoolean);
  }

  class NumberDocument extends PlainDocument
  {
    private final JNumberField this$0;

    public NumberDocument()
    {
      this.this$0 = paramJNumberField;
    }

    public void insertString(, String paramString, AttributeSet paramAttributeSet)
      throws BadLocationException
    {
      String str2;
      String str1 = getText(0, getLength());
      if ((paramString.equals("F")) || (paramString.equals("f")) || (paramString.equals("D")) || (paramString.equals("d")) || ((str1.trim().equals("0")) && (!(paramString.substring(0, 1).equals("."))) && (paramInt != 0)) || ((paramString.equals(".")) && (JNumberField.access$000(this.this$0) == 0)))
      {
        Toolkit.getDefaultToolkit().beep();
        return;
      }
      String str3 = "";
      String str4 = str1.substring(0, paramInt) + paramString + str1.substring(paramInt, getLength());
      str4 = str4.replaceFirst("-", "");
      int i = str4.indexOf(".");
      if (i > -1)
      {
        str2 = str4.substring(0, i);
        str3 = str4.substring(i + 1);
      }
      else
      {
        str2 = str4;
      }
      if ((str2.length() > JNumberField.access$100(this.this$0)) || (str3.length() > JNumberField.access$000(this.this$0)) || ((str4.length() > 1) && (str4.substring(0, 1).equals("0")) && (!(str4.substring(1, 2).equals(".")))))
      {
        Toolkit.getDefaultToolkit().beep();
        return;
      }
      try
      {
        if ((!(str4.equals(""))) && (!(str4.equals("-"))))
        {
          double d = Double.parseDouble(str4);
          if ((d < JNumberField.access$200(this.this$0)) || (d > JNumberField.access$300(this.this$0)))
            throw new Exception();
        }
      }
      catch (Exception localException)
      {
        Toolkit.getDefaultToolkit().beep();
        return;
      }
      JNumberField.access$402(this.this$0, true);
      super.insertString(paramInt, paramString, paramAttributeSet);
    }
  }
}